package com.cp.dao;

import com.cp.repository.ClassRelation;
import com.cp.repository.CodeSemanticChunk;
import com.cp.repository.Conviction;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Mapper
public interface CodeSemanticChunkMapper {


	@Select("SELECT * FROM code_semantic_chunk")
	@Results({
		@Result(id = true, property = "id", column = "id"),
		@Result(property = "content", column = "content"),
		@Result(property = "conviction", column = "conviction_id", one = @One(select="com.cp.dao.ConvictionMapper.getById")),
        @Result(property = "classRelation", column = "class_relation_id", one = @One(select = "com.cp.dao.ClassRelationMapper.getById"))
	})
	List<CodeSemanticChunk> getAll();

	@Select("SELECT DISTINCT(conviction_id) FROM code_semantic_chunk WHERE content = #{semanticChunk}")
	@Results({
			@Result(property = "content", column = "content"),
			@Result(property = "conviction", column = "conviction_id", one = @One(select="com.cp.dao.ConvictionMapper.getById")),
            @Result(property = "classRelation", column = "class_relation_id", one = @One(select = "com.cp.dao.ClassRelationMapper.getById"))
	})
	List<CodeSemanticChunk> getBySemanticChunk(String semanticChunk);

	@Select("SELECT * FROM code_semantic_chunk WHERE id = #{id}")
	@Results({
			@Result(property = "content", column = "content"),
			@Result(property = "conviction", column = "conviction_id", one = @One(select="com.cp.dao.ConvictionMapper.getById")),
			@Result(property = "classRelation", column = "class_relation_id", one = @One(select = "com.cp.dao.ClassRelationMapper.getById"))
	})
	CodeSemanticChunk getById(Integer id);

	@Select("SELECT * FROM code_semantic_chunk WHERE conviction_id = #{convictionId}")
	@Results({
			@Result(property = "content", column = "content"),
			@Result(property = "conviction", column = "conviction_id", one = @One(select="com.cp.dao.ConvictionMapper.getById")),
			@Result(property = "classRelation", column = "class_relation_id", one = @One(select = "com.cp.dao.ClassRelationMapper.getById"))
	})
	List<CodeSemanticChunk> getByConvictionId(Integer convictionId);

	@Select("<script>SELECT * FROM code_semantic_chunk WHERE  <if test='classRelationId != null'> class_relation_id = #{classRelationId} AND </if>  content LIKE '%${semanticChunk}%'</script>")
	@Results({
			@Result(property = "content", column = "content"),
			@Result(property = "conviction", column = "conviction_id", one = @One(select="com.cp.dao.ConvictionMapper.getById")),
			@Result(property = "classRelation", column = "class_relation_id", one = @One(select = "com.cp.dao.ClassRelationMapper.getById"))
	})
	List<CodeSemanticChunk> getBySemanticChunkAndClassRelationId(@Param("semanticChunk") String semanticChunk, @Param("classRelationId")Integer classRelationId);


	@Select("<script>SELECT * FROM code_semantic_chunk WHERE 1 = 1 <if test='classRelationId != 0'> AND class_relation_id = #{classRelationId} </if> " +
			"<if test='convictionId != 0'> AND conviction_id = #{convictionId} </if></script>")
	@Results({
			@Result(property = "content", column = "content"),
			@Result(property = "conviction", column = "conviction_id", one = @One(select="com.cp.dao.ConvictionMapper.getById")),
			@Result(property = "classRelation", column = "class_relation_id", one = @One(select = "com.cp.dao.ClassRelationMapper.getById"))
	})
	List<CodeSemanticChunk> getByConvictionIdOrClassRelationId(@Param("convictionId") Integer convictionId, @Param("classRelationId") Integer classRelationId, @Param("pageNumber")Integer pageNumber);

}
